package 可重复发送;

import java.io.*;
import java.net.Socket;

/**
 * 在线聊天室：客户端
 * 目标：实现以一个客户收发多条消息
 */
public class Client {
    public static void main(String[] args) throws IOException {
        //1.指定服务器端的IP和端口号
        System.out.println("------------Client------------");
        Socket socket = new Socket("localhost",8888);
        //2.向服务器发送信息
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        boolean isRunning = true;
        while (isRunning){
            String mag = console.readLine();
            dos.writeUTF(mag);
            dos.flush();
            //3.接收服务器的消息
            mag = dis.readUTF();
            System.out.println(mag);
            if (mag == null){
                isRunning = false;
            }
        }
        //4.关闭、释放资源
        socket.close();
        dos.close();
        dis.close();
        console.close();
    }
}
